﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Collections.Generic;

namespace OrderService_Test
{
    [TestClass]
    public class UnitTest1
    {
        private OrderService Order_All;

        [TestInitialize]
        public void Test_Initialize()
        {
            Order_All = new OrderService();
            Order_All.import();
        }

        [TestMethod]
        public void Test_Add_Order()
        {
            OrderDetail detail_1 = new OrderDetail("橘子", 10, 5);
            OrderDetail detail_2 = new OrderDetail("香蕉", 20, 2);
            Order order_test = new Order()
            {
                Order_ID = "085",
                Customer = "小猪",
                Order_Time = DateTime.Parse("2023-04-08"),
                Details = new List<OrderDetail>() { detail_1, detail_2 }
            };
            Order_All.Add_Order(order_test);

            Assert.IsTrue(Order_All.Orders.Contains(order_test));
        }

        [TestMethod]
        public void Test_Remove_Order()
        {
            // 删除订单
            Order_All.Remove_Order("001");

            // 判断订单是否删除成功
            Assert.IsFalse(Order_All.Orders.Any(o => o.Order_ID == "001"));
        }

        [TestMethod]
        public void Test_Update_Order()
        {
            OrderDetail detail_1 = new OrderDetail("商品1", 10, 2);
            OrderDetail detail_2 = new OrderDetail("商品2", 20, 3);

            Order order_test = new Order()
            {
                Order_ID = "001",
                Customer = "小汪",
                Order_Time = DateTime.Parse("2022-03-08"),
                Details = new List<OrderDetail>() { detail_1, detail_2 }
            };

            // 修改订单
            Order_All.Update_Order(order_test);

            // 判断订单是否修改成功
            Assert.IsTrue(Order_All.Orders.Contains(order_test));
        }

        [TestMethod]
        public void Test_QueryOrdersBy_OrderId()
        {
            // 按照订单号查询订单
            List<Order> result = Order_All.QueryOrdersBy_OrderId("001");

            // 判断查询结果是否正确
            Assert.AreEqual(result.Count, 1);
            Assert.AreEqual(result[0].Order_ID, "001");
        }

        [TestMethod]
        public void Test_QueryOrdersBy_ItemName()
        {
            // 按照商品名称查询订单
            List<Order> result = Order_All.QueryOrdersBy_ItemName("苹果");

            // 判断查询结果是否正确
            Assert.AreEqual(result.Count, 1);
            Assert.AreEqual(result[0].Order_ID, "004");
        }

        [TestMethod]
        public void TestQueryOrdersByCustomer()
        {
            // 按照客户查询订单
            List<Order> result = Order_All.QueryOrdersBy_Customer("小汪");

            // 判断查询结果是否正确
            Assert.AreEqual(result.Count, 1);
            Assert.AreEqual(result[0].Customer, "小汪");
        }

        [TestMethod]
        public void TestQueryOrdersByTotalAmount()
        {
            // 按照订单金额查询订单
            List<Order> result = Order_All.QueryOrdersBy_TotalAmount(100000);

            // 判断查询结果是否正确
            Assert.AreEqual(result.Count, 0);
        }

        [TestMethod]
        public void TestSortOrders()
        {
            // 按照订单号排序
            Order_All.Sort_Orders("Order_ID");

            // 判断排序结果是否正确
            Assert.AreEqual(Order_All.Orders[0].Order_ID, "001");
            Assert.AreEqual(Order_All.Orders[1].Order_ID, "002");
            Assert.AreEqual(Order_All.Orders[2].Order_ID, "003");
            Assert.AreEqual(Order_All.Orders[3].Order_ID, "004");

            // 按照订单金额排序
            Order_All.Sort_Orders("Total_Amount");

            // 判断排序结果是否正确
            Assert.AreEqual(Order_All.Orders[0].Order_ID, "001");
            Assert.AreEqual(Order_All.Orders[1].Order_ID, "003");
            Assert.AreEqual(Order_All.Orders[2].Order_ID, "002");
            Assert.AreEqual(Order_All.Orders[3].Order_ID, "004");
        }

    }
}
